package com.atmilan.quartz.sync.pull.buyer.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atmilan.quartz.model.entity.buyer.BuyerProduct;
import com.atmilan.quartz.sync.pull.buyer.mapper.BuyerProductMapper;
import com.atmilan.quartz.sync.pull.buyer.service.IBuyerProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hupeng
 * @since 2020-04-27
 */
@Slf4j
@Service
@Transactional
public class BuyerProductServiceImpl extends ServiceImpl<BuyerProductMapper, BuyerProduct> implements IBuyerProductService {
    @Autowired
    BuyerProductMapper buyerProductMapper;

    @Override
    public List<BuyerProduct> getDeleteList(Date startTime) {
        return this.baseMapper.selectList(Wrappers.<BuyerProduct>lambdaQuery().lt(BuyerProduct::getCreateTime, startTime));
    }

    @Override
    public List<String> allGoodsIdList() {
        List<Object> list = buyerProductMapper.selectObjs(new QueryWrapper<BuyerProduct>().select("goods_id"));
        return list == null ? new ArrayList<String>() : list.stream().map(o -> o.toString()).collect(Collectors.toList());
    }

    @Override
    public List<String> findDeleteGoodsIdList(Date startTime) {
        return buyerProductMapper.findDeleteGoodsIds(startTime);
    }

    @Override
    public boolean replaceIntoProduct(BuyerProduct buyerProduct) {
        if (buyerProduct == null || StringUtils.isBlank(buyerProduct.getGoodsId())) {
            log.warn("goodId is null ! product: {}", buyerProduct);
            return false;
        }
        return buyerProductMapper.replaceIntoProduct(buyerProduct) > 0;
    }

    @Override
    public BuyerProduct getByGoodsId(String goodsId) {
        return buyerProductMapper.selectOne(Wrappers.<BuyerProduct>lambdaQuery().eq(BuyerProduct::getGoodsId, goodsId));
    }

    @Override
    public void deleteAll() {
        buyerProductMapper.deleteAll();
    }
    
    @Override
    public void copyAll() {
        buyerProductMapper.deleteProductBack();
        buyerProductMapper.copyProductBack();
    }
    
}
